#include "stdafx.h"
#include "luaHelp.h"
using namespace luaHelp;

LuaTable::LuaTable( lua_State*L ) :mL(L)
{
	lua_newtable( mL );
	mIndex = lua_gettop(mL);
}
LuaTable::LuaTable( const LuaTable& t )
 :mL(t.mL),mIndex(t.mIndex)
{

}
LuaTable::LuaTable( lua_State*L, int index ) :mL(L),mIndex(index)
{
	luaplus_assert(L);
	luaplus_assert(lua_istable(L,index));
}
LuaTable& LuaTable::operator=( const LuaTable& t )
{
	mL = t.mL;
	mIndex = t.mIndex;
	return *this;
}

void		LuaTable::GetValue(const char* pkey,bool &value)
{
	lua_pushstring(mL,pkey);
	lua_gettable(mL,mIndex);
	luaplus_assert(lua_isboolean(mL,-1));
	value = lua_toboolean(mL,-1)?true:false;
	lua_pop(mL,1);
}
void		LuaTable::GetValue(const char* pkey,char &value)
{
	lua_pushstring(mL,pkey);
	lua_gettable(mL,mIndex);
	luaplus_assert(lua_isnumber(mL,-1));
	//luaplus_assert( abs((int)lua_tonumber(mL,-1))<128 );
	value = (char)lua_tonumber(mL,-1);	
	lua_pop(mL,1);
}
void		LuaTable::GetValue(const char* pkey,unsigned char &value)
{
	lua_pushstring(mL,pkey);
	lua_gettable(mL,mIndex);
	luaplus_assert(lua_isnumber(mL,-1));
	luaplus_assert( lua_tonumber(mL,-1)<256 );
	value = (unsigned char)lua_tonumber(mL,-1);	
	lua_pop(mL,1);
}
void		LuaTable::GetValue(const char* pkey,short &value)
{
	lua_pushstring(mL,pkey);
	lua_gettable(mL,mIndex);
	luaplus_assert(lua_isnumber(mL,-1));
	value = (short)lua_tonumber(mL,-1);	
	lua_pop(mL,1);
}
void		LuaTable::GetValue(const char* pkey,unsigned short &value)
{
	lua_pushstring(mL,pkey);
	lua_gettable(mL,mIndex);
	luaplus_assert(lua_isnumber(mL,-1));
	value = (unsigned short)lua_tonumber(mL,-1);	
	lua_pop(mL,1);
}
void		LuaTable::GetValue(const char* pkey,int &value)
{
	lua_pushstring(mL,pkey);
	lua_gettable(mL,mIndex);
	luaplus_assert(lua_isnumber(mL,-1));
	value = (int)lua_tonumber(mL,-1);	
	lua_pop(mL,1);
}
void		LuaTable::GetValue(const char* pkey,unsigned int &value)
{
	lua_pushstring(mL,pkey);
	lua_gettable(mL,mIndex);
	luaplus_assert(lua_isnumber(mL,-1));
	value = (unsigned int)lua_tonumber(mL,-1);
	lua_pop(mL,1);
}
void		LuaTable::GetValue(const char* pkey,long &value)
{
	lua_pushstring(mL,pkey);
	lua_gettable(mL,mIndex);
	luaplus_assert(lua_isnumber(mL,-1));
	value = (long)lua_tonumber(mL,-1);
	lua_pop(mL,1);
}
void		LuaTable::GetValue(const char* pkey,unsigned long &value)
{
	lua_pushstring(mL,pkey);
	lua_gettable(mL,mIndex);
	luaplus_assert(lua_isnumber(mL,-1));
	value = (unsigned long)lua_tonumber(mL,-1);
	lua_pop(mL,1);
}
void		LuaTable::GetValue(const char* pkey,double &value)
{
	lua_pushstring(mL,pkey);
	lua_gettable(mL,mIndex);
	luaplus_assert(lua_isnumber(mL,-1));
	value = (double)lua_tonumber(mL,-1);
	lua_pop(mL,1);
}
void		LuaTable::GetValue(const char* pkey,float &value)
{
	lua_pushstring(mL,pkey);
	lua_gettable(mL,mIndex);
	luaplus_assert(lua_isnumber(mL,-1));
	value = (float)lua_tonumber(mL,-1);
	lua_pop(mL,1);
}
void		LuaTable::GetValue(const char* pkey,std::string &value)
{
	lua_pushstring(mL,pkey);
	lua_gettable(mL,mIndex);
	luaplus_assert(lua_isnumber(mL,-1));
	const char* pstr = (char*)lua_tostring(mL,-1);
	if( pstr )
		value = pstr;
	else
		value = "";
	lua_pop(mL,1);
}
void		LuaTable::GetValue(const char* pkey,lua_CFunction &value)	
{
	lua_pushstring(mL,pkey);
	lua_gettable(mL,mIndex);
	luaplus_assert(lua_isfunction(mL,-1));
	value = lua_tocfunction(mL,-1);
	lua_pop(mL,1);
}

void		LuaTable::GetValue(const char* pkey,LuaLightUserData &value)
{
	lua_pushstring(mL,pkey);
	lua_gettable(mL,mIndex);
	value.m_value = lua_touserdata(mL,-1);
	lua_pop(mL,1);
}
void		LuaTable::GetValue(const char* pkey,LuaUserData &value)
{
	lua_pushstring(mL,pkey);
	lua_gettable(mL,mIndex);
	value.m_value = lua_touserdata(mL,-1);
	lua_pop(mL,1);
}

void		LuaTable::SetValue(const char* pkey,bool value)
{
	lua_pushboolean(mL,value?1:0);
	lua_setfield(mL,mIndex,pkey);
}
void		LuaTable::SetValue(const char* pkey,char value)
{
	lua_pushlstring(mL,&value,sizeof(char));
	lua_setfield(mL,mIndex,pkey);
}
void		LuaTable::SetValue(const char* pkey,unsigned char value)
{
	lua_pushnumber(mL,(lua_Number)value);
	lua_setfield(mL,mIndex,pkey);
}
void		LuaTable::SetValue(const char* pkey,short value)
{
	lua_pushnumber(mL,(lua_Number)value);
	lua_setfield(mL,mIndex,pkey);
}
void		LuaTable::SetValue(const char* pkey,unsigned short value)
{
	lua_pushnumber(mL,(lua_Number)value);
	lua_setfield(mL,mIndex,pkey);
}
void		LuaTable::SetValue(const char* pkey,int value)
{
	lua_pushnumber(mL,(lua_Number)value);
	lua_setfield(mL,mIndex,pkey);
}
void		LuaTable::SetValue(const char* pkey,unsigned int value)
{
	lua_pushnumber(mL,(lua_Number)value);
	lua_setfield(mL,mIndex,pkey);
}
void		LuaTable::SetValue(const char* pkey,long value)
{
	lua_pushnumber(mL,(lua_Number)value);
	lua_setfield(mL,mIndex,pkey);
}
void		LuaTable::SetValue(const char* pkey,unsigned long value)
{
	lua_pushnumber(mL,(lua_Number)value);
	lua_setfield(mL,mIndex,pkey);
}
void		LuaTable::SetValue(const char* pkey,double value)
{
	lua_pushnumber(mL,(lua_Number)value);
	lua_setfield(mL,mIndex,pkey);
}
void		LuaTable::SetValue(const char* pkey,float value)
{
	lua_pushnumber(mL,(lua_Number)value);
	lua_setfield(mL,mIndex,pkey);
}
void		LuaTable::SetValue(const char* pkey,char* value)
{
	lua_pushstring(mL,value);
	lua_setfield(mL,mIndex,pkey);
}
void		LuaTable::SetValue(const char* pkey,lua_CFunction value)
{
	lua_pushcfunction(mL,value);
	lua_setfield(mL,mIndex,pkey);
}
void		LuaTable::SetValue(const char* pkey,void* value)
{
	lua_pushlightuserdata(mL,value);
	lua_setfield(mL,mIndex,pkey);
}
void		LuaTable::SetValue(const char* pkey,const LuaNil& value)
{
	lua_pushnil(mL);
	lua_setfield(mL,mIndex,pkey);
}
void		LuaTable::Dump( const char* file )
{
	DebugOutput* pDebug = 0;
	if( file==0 )
		pDebug = new DebugOut();
	else
		pDebug = new FileOut(file);

	lua_State* L = mL;
	lua_pushnil(L);  /* first key */
	while (lua_next(L, mIndex) != 0) {
		/* `key' is at index -2 and `value' at index -1 */
		int t = lua_type(L, -1); 
		switch (t) 
		{ 
		case LUA_TSTRING:  /* strings */ 
			pDebug->WriteString("%s=%s", lua_tostring(L, -2),lua_tostring(L, -1)); 
			break; 
		case LUA_TBOOLEAN:  /* booleans */ 
			pDebug->WriteString("%s=%s", lua_tostring(L, -2),(int)lua_tonumber(L, -1)==0?"true":"false"); 
			break; 
		case LUA_TNUMBER:  /* numbers */ 
			pDebug->WriteString("%s=%g", lua_tostring(L, -2),lua_tonumber(L, -1)); 
			break; 
		case LUA_TFUNCTION:
			pDebug->WriteString("%s=%s", lua_tostring(L, -2),lua_typename(L, t)); 
			break;
		default:  /* other values */ 
			pDebug->WriteString("%s=%s", lua_tostring(L, -2),lua_typename(L, t)); 
			break; 
		} 
		pDebug->WriteString("\n");  /* put a separator */ 
		lua_pop(L, 1);  /* removes `value'; keeps `key' for next iteration */
	}

	delete pDebug;
	pDebug = 0;
}